package org.vy.dal.sample;

import org.vy.dal.QueryManager;

public class TestDAO {
	
	private static final String FIND_BY_PK = "SELECT * FROM <TABLE> WHERE ID = m_id";
	private static final String UPDATE = "UPDATE <TABLE> SET NAME = m_name WHERE ID = m_id";
	private static final String INSERT = "INSERT INTO <TABLE> (ID,NAME) VALUES(m_id,m_name)";
	
	public static TestDO findByPK(long key) throws Exception {
		TestDO dto = new TestDO();
		dto.setId(key);
		dto = (TestDO)QueryManager.executeQuery(dto, FIND_BY_PK);
		return dto;
	}

	public static TestDO insert(TestDO dto) throws Exception {
		dto.setId(TestCommonSeqGenerator.getInstance().getNextSequence());
		dto = (TestDO)QueryManager.executeInsert(dto, INSERT);
		return dto;
	}

	public static TestDO update(TestDO dto) throws Exception {
		int i = QueryManager.executeUpdate(dto, UPDATE);
		if(i > 0)
			return dto;
		else
			return null;
	}
	
	public static void main(String[] s){
		try{
			TestDO td = new TestDO();
			td.setName("Raj");
			TestDAO.insert(td);
			System.out.println("Seq Id = "+ td.getId());
			
			td.setName(null);
			td = TestDAO.findByPK(td.getId());
			System.out.println("Query Name = "+ td.getName());
			
			td.setName("Gupta");
			td = TestDAO.update(td);
			if(td != null)
				System.out.println("Update Name = "+ td.getName());
			else
				System.out.println("Update Failed");
			
		}catch(Exception e){
			e.printStackTrace();
		}
		
		
	}

}
